const findDuplicate = (nums) => {
  const len = nums.length;
  let left = 1;
  let right = len - 1;
  while (left < right) {
    let mid = (left + right) >> 1;
    let cnt = 0;
    for (const num of nums) {
      if (num <= mid) {
        cnt += 1;
      }
    }
    // 根据抽屉原理，小于等于 4 的个数如果严格大于 4 个
    // 此时重复元素一定出现在 [1,4] 区间里
    if (cnt > mid) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return left;
};
